<?php

namespace app\api\controller\kuerp\order;

use app\common\controller\Api;
use app\api\model\kuerp\Systems as Systems;

/**
 * 系统配置接口
 */
class Config extends Api
{
    protected $noNeedRight = '*';
    protected $noNeedLogin = '*';

    /**
     * 获取系统配置详情
     * @ApiMethod (POST)
     * @param int $id 系统配置ID
     */
    public function getdetail()
    {
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $result = Systems::getDetail($name);
            $this->success(__('Query successful'), $result, 200);
        } else {
            $this->error(__('Request method error'), '', 250);
        }
    }

    /**
     * 添加系统配置
     * @ApiMethod (POST)
     * @param array $detail 系统配置数据
     */
    public function adddetail()
    {
        if ($this->request->isPost()) {
            $detail = $this->request->post();
            foreach ($detail as $k => $v) {
                $detail[$k] = is_array($v) ? $v : html_entity_decode($v);
            }
            $ret = Systems::addDetail($detail);
            if ($ret['status'] == 200) {
                $this->success(__('Added successfully'), $ret['msg'], 200);
            } else {
                $this->error($ret['msg'], '', $ret['status']);
            }
        } else {
            $this->error(__('Request method error'), '', 250);
        }
    }

    /**
     * 更新系统配置
     * @ApiMethod (POST)
     * @param array $detail 系统配置数据
     */
    public function editdetail()
    {
        if ($this->request->isPost()) {
            $detail = $this->request->post();
            foreach ($detail as $k => $v) {
                $detail[$k] = is_array($v) ? $v : html_entity_decode($v);
            }
            $ret = Systems::editDetail($detail);
            if ($ret['status'] == 200) {
                $this->success(__('Updated successfully'), '', 200);
            } else {
                $this->error($ret['msg'], '', $ret['status']);
            }
        } else {
            $this->error(__('Request method error'), '', 250);
        }
    }

    /**
     * 系统配置
     * @ApiMethod (POST)
     * @param int $name 系统配置name
     */
    public function getSet()
    {
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $ret = \think\Db::name('kuerp_user_config')->where(['name' => $name])->find();
            if ($ret) {
                $this->success(__('Query successful'), $ret['value'], 200);
            } else {
                $ret = \think\Db::name('kuerp_user_sets')->where(['name' => $name])->find();
                if ($ret) {
                    $this->success(__('Query successful'), $ret['value'], 200);
                } else {
                    $this->error(__('Query failed'), '', 400);
                }
            }
            $this->success(__('Query successful'), $ret, 200);
        } else {
            $this->error(__('Request method error'), '', 250);
        }
    }

    /**
     * 系统配置
     * @ApiMethod (POST)
     * @param int $name 系统配置name
     */
    public function getConfig()
    {
        if ($this->request->isPost()) {
            $name = $this->request->post('name');
            $ret = \think\Db::name('kuerp_user_sets')->where(['name' => ['in', $name]])->column('value', 'name');
            if ($ret) {
                foreach ($ret as $ret_k => $ret_v) {
                    if ($ret_k == 'wxcode') {
                        $ret[$ret_k] = $ret_v ? cdnurl($ret_v, true) : '';
                    }
                }
                $this->success(__('Query successful'), $ret, 200);
            } else {
                $this->error(__('Query failed'), '', 400);
            }
        } else {
            $this->error(__('Request method error'), '', 250);
        }
    }
}
